/**
 @Name：大学生计算机课程平时成绩管理系统 课程表管理
 @Author：dingwen
 */

layui.define(['table', 'form'], function (exports) {
    let $ = layui.$
        , table = layui.table
        , setter = layui.setter;

    let total = 0;

    $(function () {
        /*获取总记录条数*/
        $.ajax({
            url: layui.setter.baseUrl + '/schedule/total',
            type: 'get',
            dateType: "json",
            contentType: 'application/json',
            success: function (res) {
                if (res.code === 200) {
                    total = res.data;
                    // 表格数据渲染
                    table.render({
                        elem: '#LAY-schedule-manage'
                        , page: true
                        , url: layui.setter.baseUrl + '/schedule/info/page'
                        , toolbar: '#toolbarDemo'
                        , title: '课程表'
                        , parseData: function (res) {
                            return {
                                "code": 0,
                                "msg": res.message,
                                "count": total,
                                "data": res.data
                            };
                        }
                        , request: {
                            pageName: 'pageNum'
                            , limitName: 'pageSize'
                        }
                        , cols:
                            [[
                                {type: 'checkbox', fixed: 'left'}
                                , {field: 'scheduleId', width: 200, title: 'ID', sort: true}
                                , {field: 'courseId', width: 100, title: '课程编号',}
                                , {field: 'courseName', width: 160, title: '课程名称',}
                                , {field: 'clazzId', width: 160, title: '班级编号'}
                                , {field: 'clazzName', width: 160, title: '班级名称'}
                                , {field: 'clazzYear', width: 100, title: '班级年限'}
                                , {field: 'scheduleYear', width: 100, title: '学年'}
                                , {field: 'scheduleType', width: 100, title: '学期', templet: '#schedule-type'}
                                , {field: 'courseType', width: 200, title: '类型', templet: '#course-type'}
                                , {field: 'courseAddress', title: '授课地址', minWidth: 200}
                                , {field: 'courseTime', title: '课时', minWidth: 200}
                                , {field: 'teacherName', width: 100, title: '授课教师',}
                                , {field: 'teacherId', width: 160, title: '教工号',}
                                , {field: 'teacherWechat', title: '授课教师电话', width: 200}
                                , {
                                    field: 'scheduleTimeStart',
                                    width: 200,
                                    title: '上课时间起',
                                    style: 'background-color: #e74c3c; color: #fff;',
                                }
                                , {
                                    field: 'scheduleTimeEnd',
                                    width: 200,
                                    title: '上课时间止',
                                    style: 'background-color: #3498db; color: #fff;',
                                }
                                , {
                                    title: '操作',
                                    width: 200,
                                    align: 'center',
                                    fixed: 'right',
                                    toolbar: '#layuiadmin-app-schedule-bar'
                                }
                            ]]
                    });
                } else {
                    layer.msg("失败");
                }
            }
        });
    });

    //监听
    table.on('tool(LAY-schedule-manage)', function (obj) {
        let data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除？', function (index) {
                $.ajax({
                    url: setter.baseUrl + `/schedule/remove/${data.scheduleId}`,
                    dataType: "json",
                    type: "delete",
                    contentType: 'application/json',
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg("成功");
                        } else {
                            layer.msg("失败");
                        }
                    },
                    error: function (e) {
                        layer.msg("失败");
                    }
                });
                obj.del();
                layer.close(index);
            });
        }
    });
    exports('schedule', {})
});
